using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    partial class GSUResultsDialog : Form
    {
        private struct GSUCombatCasualtySelection
        {
            public GroundSupportUnit GSU;
            public bool groundSupporting;
            public bool casualty;
        };

        List<GSUCombatCasualtySelection> attackerGSUList;
        List<GSUCombatCasualtySelection> defenderGSUList;
        List<int> attackingDieRolls;
        List<int> defendingDieRolls;
        bool shownForAttacker;
        int numHitsToAllocate;
        Bitmap hitBitmap;

        public GSUResultsDialog(List<GroundSupportUnit> attackerGSUList, List<GroundSupportUnit> defenderGSUList, List<int> attackerDieRolls, List<int> defenderDieRolls, Dictionary<MapInfo.CountryID,int> defenderGSUSelections, Dictionary<MapInfo.CountryID, int> attackerGSUSelections, bool shownForAttacker, MapInfo.RegionID battleRegion)
        {
            Dictionary<MapInfo.CountryID,int> defenderGSUSelectionsToModify = new Dictionary<MapInfo.CountryID,int>(defenderGSUSelections);
            Dictionary<MapInfo.CountryID, int> attackerGSUSelectionsToModify = new Dictionary<MapInfo.CountryID, int>(attackerGSUSelections);
            InitializeComponent();
            this.attackerGSUList = new List<GSUCombatCasualtySelection>();
            this.defenderGSUList = new List<GSUCombatCasualtySelection>();
            this.attackingDieRolls = new List<int>(attackerDieRolls);
            this.defendingDieRolls = new List<int>(defenderDieRolls);
            this.shownForAttacker = shownForAttacker;
            this.Text = "GSU Combat in " + Enum.GetName(battleRegion.GetType(), battleRegion);
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;
            file = thisExe.GetManifestResourceStream("EuropeEngulfed.NET.Resources.Hits.bmp");
            hitBitmap = (Bitmap)Bitmap.FromStream(file);
            hitBitmap.MakeTransparent(System.Drawing.Color.FromArgb(128,128,128));
            file.Dispose();
            if(shownForAttacker)
            {
                foreach(int dieRoll in defenderDieRolls)
                {
                    if (dieRoll >= 5)
                        numHitsToAllocate++;
                }

                if (numHitsToAllocate > attackerGSUList.Count)
                    numHitsToAllocate = attackerGSUList.Count;
            }
            else
            {
                foreach (int dieRoll in attackerDieRolls)
                {
                    if (dieRoll >= 5)
                        numHitsToAllocate++;
                }

                if (numHitsToAllocate > defenderGSUList.Count)
                    numHitsToAllocate = defenderGSUList.Count;
            }

            if (numHitsToAllocate == 1)
                this.Text = this.Text + " - Allocate " + numHitsToAllocate + " hit";
            else if(numHitsToAllocate > 1)
                this.Text = this.Text + " - Allocate " + numHitsToAllocate + " hits";

            foreach(GroundSupportUnit attackingGSU in attackerGSUList)
            {
                GSUCombatCasualtySelection combatCasualtySelectionToAdd = new GSUCombatCasualtySelection();
                combatCasualtySelectionToAdd.GSU = attackingGSU;
                combatCasualtySelectionToAdd.casualty = false;

                if(attackerGSUSelectionsToModify.ContainsKey(attackingGSU.CountryOwner))
                {
                    if (attackerGSUSelectionsToModify[attackingGSU.CountryOwner] > 0)
                    {
                        combatCasualtySelectionToAdd.groundSupporting = true;
                        attackerGSUSelectionsToModify[attackingGSU.CountryOwner]--;
                    }
                    else
                        combatCasualtySelectionToAdd.groundSupporting = false;
                }
                else
                    combatCasualtySelectionToAdd.groundSupporting = false;

                this.attackerGSUList.Add(combatCasualtySelectionToAdd);
            }

            foreach (GroundSupportUnit defendingGSU in defenderGSUList)
            {
                GSUCombatCasualtySelection combatCasualtySelectionToAdd = new GSUCombatCasualtySelection();
                combatCasualtySelectionToAdd.GSU = defendingGSU;
                combatCasualtySelectionToAdd.casualty = false;
                if (defenderGSUSelectionsToModify.ContainsKey(defendingGSU.CountryOwner))
                {
                    if (defenderGSUSelectionsToModify[defendingGSU.CountryOwner] > 0)
                    {
                        combatCasualtySelectionToAdd.groundSupporting = true;
                        defenderGSUSelectionsToModify[defendingGSU.CountryOwner]--;
                    }
                    else
                        combatCasualtySelectionToAdd.groundSupporting = false;
                }
                else
                    combatCasualtySelectionToAdd.groundSupporting = false;

                this.defenderGSUList.Add(combatCasualtySelectionToAdd);
            }

            this.attackerGSUListView.View = View.Tile;
            this.attackerGSUListView.TileSize = new Size(PlayingPieceGraphics.GetGSUSize().Width + PlayingPieceGraphics.GetGSUSize().Width / 3, PlayingPieceGraphics.GetGSUSize().Height + PlayingPieceGraphics.GetGSUSize().Height / 3);
            foreach (GSUCombatCasualtySelection attackingGSU in this.attackerGSUList)
            {
                this.attackerGSUListView.Items.Add(attackingGSU.GSU.GetDescription());
            }

            this.defenderGSUListView.View = View.Tile;
            this.defenderGSUListView.TileSize = new Size(PlayingPieceGraphics.GetGSUSize().Width + PlayingPieceGraphics.GetGSUSize().Width / 3, PlayingPieceGraphics.GetGSUSize().Height + PlayingPieceGraphics.GetGSUSize().Height / 3);
            foreach (GSUCombatCasualtySelection defendingGSU in this.defenderGSUList)
            {
                this.defenderGSUListView.Items.Add(defendingGSU.GSU.GetDescription());
            }

            this.listViewAttackerDieRolls.View = View.Tile;
            this.listViewAttackerDieRolls.TileSize = new Size(PlayingPieceGraphics.GetDieSize().Width + PlayingPieceGraphics.GetDieSize().Width / 3, PlayingPieceGraphics.GetDieSize().Height + PlayingPieceGraphics.GetDieSize().Height / 3);
            foreach (int dieRoll in attackingDieRolls)
            {
                this.listViewAttackerDieRolls.Items.Add(dieRoll.ToString());
            }

            this.listViewdefenderDieRolls.View = View.Tile;
            this.listViewdefenderDieRolls.TileSize = new Size(PlayingPieceGraphics.GetDieSize().Width + PlayingPieceGraphics.GetDieSize().Width / 3, PlayingPieceGraphics.GetDieSize().Height + PlayingPieceGraphics.GetDieSize().Height / 3);
            foreach (int dieRoll in defendingDieRolls)
            {
                this.listViewdefenderDieRolls.Items.Add(dieRoll.ToString());
            }
        }

        public Dictionary<MapInfo.CountryID, int> GetCombatCasualtySelections()
        {
            Dictionary<MapInfo.CountryID, int> combatCasualtiesToReport = new Dictionary<MapInfo.CountryID, int>();
            List<GSUCombatCasualtySelection> combatCasualtySelectionListToReportOn = shownForAttacker? attackerGSUList : defenderGSUList;

            foreach(GSUCombatCasualtySelection combatCasualtySelection in combatCasualtySelectionListToReportOn)
            {
                if(combatCasualtySelection.casualty)
                {
                    if (!combatCasualtiesToReport.ContainsKey(combatCasualtySelection.GSU.CountryOwner))
                        combatCasualtiesToReport.Add(combatCasualtySelection.GSU.CountryOwner, 1);
                    else
                        combatCasualtiesToReport[combatCasualtySelection.GSU.CountryOwner]++;
                }
            }

            return combatCasualtiesToReport;
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            int numGSUCasualties = 0;
            if(shownForAttacker)
            {
                foreach (GSUCombatCasualtySelection gsuSelection in this.attackerGSUList)
                {
                    if (gsuSelection.casualty)
                        numGSUCasualties++;
                }
            }
            else
            {
                foreach (GSUCombatCasualtySelection gsuSelection in this.defenderGSUList)
                {
                    if (gsuSelection.casualty)
                        numGSUCasualties++;
                }
            }

            if (numGSUCasualties != this.numHitsToAllocate)
                MessageBox.Show("You must allocate " + this.numHitsToAllocate + " hit(s)");
            else
                this.Close();
        }

        private void attackerGSUListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            PlayingPieceGraphics.PaintGSU(e.Graphics, attackerGSUList[e.ItemIndex].GSU, new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)), !attackerGSUList[e.ItemIndex].groundSupporting);
            if(shownForAttacker)
            {
                if(attackerGSUList[e.ItemIndex].casualty)
                {
                    e.Graphics.DrawImage(hitBitmap, new Point(e.Bounds.Left, e.Bounds.Top));
                }
            }
        }

        private void listViewAttackerDieRolls_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            PlayingPieceGraphics.PaintHitDie(e.Graphics, new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)), this.attackingDieRolls[e.ItemIndex], this.attackingDieRolls[e.ItemIndex] >= 5);
        }

        private void defenderGSUListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            PlayingPieceGraphics.PaintGSU(e.Graphics, defenderGSUList[e.ItemIndex].GSU, new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)), !defenderGSUList[e.ItemIndex].groundSupporting);
            if (!shownForAttacker)
            {
                if (defenderGSUList[e.ItemIndex].casualty)
                {
                    e.Graphics.DrawImage(hitBitmap, new Point(e.Bounds.Left, e.Bounds.Top));
                }
            }
        }

        private void listViewdefenderDieRolls_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            PlayingPieceGraphics.PaintHitDie(e.Graphics, new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)), this.defendingDieRolls[e.ItemIndex], this.defendingDieRolls[e.ItemIndex] >= 5);
        }

        private void attackerGSUListView_MouseClick(object sender, MouseEventArgs e)
        {
            if (shownForAttacker)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int numGSUCasualties = 0;
                    foreach (GSUCombatCasualtySelection gsuSelection in this.attackerGSUList)
                    {
                        if (gsuSelection.casualty)
                            numGSUCasualties++;
                    }

                    //Can't toggle if there are already the maximum number of GSUs casualties
                    if (attackerGSUList[attackerGSUListView.SelectedItems[0].Index].casualty || numGSUCasualties < this.numHitsToAllocate)
                    {
                        GSUCombatCasualtySelection selectionToChange = attackerGSUList[attackerGSUListView.SelectedItems[0].Index];
                        selectionToChange.casualty = !selectionToChange.casualty;
                        attackerGSUList[attackerGSUListView.SelectedItems[0].Index] = selectionToChange;
                        attackerGSUListView.Invalidate();
                    }
                }
            }
        }

        private void defenderGSUListView_MouseClick(object sender, MouseEventArgs e)
        {
            if (!shownForAttacker)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int numGSUCasualties = 0;
                    foreach (GSUCombatCasualtySelection gsuSelection in this.defenderGSUList)
                    {
                        if (gsuSelection.casualty)
                            numGSUCasualties++;
                    }

                    //Can't toggle if there are already the maximum number of GSUs casualties
                    if (defenderGSUList[defenderGSUListView.SelectedItems[0].Index].casualty || numGSUCasualties < this.numHitsToAllocate)
                    {
                        GSUCombatCasualtySelection selectionToChange = defenderGSUList[defenderGSUListView.SelectedItems[0].Index];
                        selectionToChange.casualty = !selectionToChange.casualty;
                        defenderGSUList[defenderGSUListView.SelectedItems[0].Index] = selectionToChange;
                        defenderGSUListView.Invalidate();
                    }
                }
            }
        }
    }
}